package com.ruoyi.plus.pub.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.plus.controller.BigFileUploadController;
import com.ruoyi.plus.pub.domain.PubResources;
import com.ruoyi.plus.pub.mapper.PubResourcesMapper;
import com.ruoyi.plus.pub.service.IPubResourcesService;
import com.ruoyi.plus.utils.minio.MinIoUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;

/**
 * 资源Service业务层处理
 */
@Service
@Slf4j
public class PubResourcesServiceImpl extends ServiceImpl<PubResourcesMapper, PubResources> implements IPubResourcesService {

    @Autowired
    private PubResourcesMapper pubResourcesMapper;


    /**
     * 查询资源列表
     *
     * @param pubResources 资源
     * @return 资源
     */
    @Override
    public List<PubResources> selectPubResourcesList(PubResources pubResources) {
        return pubResourcesMapper.selectPubResourcesList(pubResources);
    }


    @SneakyThrows
    @Override
    public void removeResources(Long[] ids) {
        for (Long id : ids) {
            PubResources pubResources = this.getById(id);
            if (MinIoUtils.config.isEnabled()) {
                String minioFileName = BigFileUploadController.RESOURCE_ROOT + "/" + pubResources.getDirPath() + "/" + pubResources.getName();
                MinIoUtils.deleteFile(minioFileName);
            } else {
                String localFileName = RuoYiConfig.getProfile() + BigFileUploadController.RESOURCE_ROOT + "/" + pubResources.getDirPath() + "/" + pubResources.getName();
                if (StringUtils.isNotEmpty(localFileName)) {
                    Path filePath = Paths.get(localFileName.trim());
                    if (Files.exists(filePath)) {
                        Files.delete(filePath);
                    }
                }
            }
            this.removeById(pubResources);
        }
    }

    @Override
    public Boolean checkRepeat(String originalFilename, Long resourceClassId, Long id) {
        List<PubResources> resourcesList = pubResourcesMapper.checkRepeat(originalFilename, resourceClassId);
        resourcesList.removeAll(Collections.singleton(null));
        resourcesList.removeIf(e -> e.getId().equals(id));
        return !resourcesList.isEmpty();
    }

    @Override
    public void addOrUpdateResources(PubResources pubResources) {
        PubResources query = new PubResources();
        query.setFilePath(pubResources.getFilePath());
        List<PubResources> resources = pubResourcesMapper.selectPubResourcesList(query);
        if (resources.isEmpty()) {
            pubResourcesMapper.insert(pubResources);
        } else {
            for (PubResources resource : resources) {
                pubResources.setId(resource.getId());
                pubResourcesMapper.updateById(pubResources);
            }
        }
    }

}
